package com.xjl;

import java.lang.reflect.Array;
import java.util.*;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author：xjl
 * @Date：2023/12/26 20:46
 */
public class StreamDemo {
    public static void main(String[] args) {

//        test01();

//        test02();

//        test03();

//        test04();

//        test05();
//          test06();
//          test07();
//          test08();
//          test09();
//          test10();
//          test11();
//          test12();
//          test13();
//          test14();
//          test15();
//          test17();
//          test18();
//          test19();
//          test20();
//          test22();
//          test23();
//          test26();
//        testAnd();
//        testOr();
//        testNegate();
//        test27();
        test28();
    }
    //方法引用语法
    private static void test28(){
        List<Author> authors = getAuthors();
        Stream<Author> authorStream = authors.stream();
        StringBuilder sb =new StringBuilder();
        authorStream.map(author -> author.getName())
                .forEach(sb::append);
        System.out.println(sb);
    }
    //方法引用语法
    private static void test27() {
        List<Author> authors = getAuthors();
        Stream<Author> authorStream = authors.stream();
        authorStream.map(new Function<Author, Integer>() {
                    @Override
                    public Integer apply(Author author) {
                        return author.getAge();
                    }
                })
                .map(new Function<Integer, String>() {
                    @Override
                    public String apply(Integer age) {
                        return String.valueOf(age);
                    }
                }).forEach(new Consumer<String>() {
                    @Override
                    public void accept(String s) {
                        System.out.println(s);
                    }
                });
        //这两种方法一样
        Stream<Author> authorStream2 = authors.stream();
        authorStream2.map(Author::getAge)
                .map(String::valueOf)
                .forEach(System.out::println);
    }
    private static void testNegate() {
        List<Author> authors = getAuthors();
        //年龄不大于17
        //negate表上和上述条件相反
        authors.stream()
                .filter(new Predicate<Author>() {
                    @Override
                    public boolean test(Author author) {
                        return author.getAge()>14;
                    }
                }.negate()).forEach(author -> System.out.println(author));
    }

    private static void testOr() {
        List<Author> authors = getAuthors();
        //年龄大于17或者名字长度小于2
        authors.stream()
                .filter(new Predicate<Author>() {
                    @Override
                    public boolean test(Author author) {
                        return author.getAge()>17;
                    }
                }.or(new Predicate<Author>() {
                    @Override
                    public boolean test(Author author) {
                        return author.getName().length()<2;
                    }
                })).forEach(author -> System.out.println(author));
    }

    private static void testAnd() {
        List<Author> authors = getAuthors();
        //年龄大于17并且名字长度大于1
//        authors.stream()
//                .filter(author -> author.getAge()>17)
//                .filter(author -> author.getName().length()>1)
//                .forEach(author -> System.out.println(author));



//        authors.stream()
//                .filter(new Predicate<Author>() {
//                    @Override
//                    public boolean test(Author author) {
//                        return author.getAge()>17&&author.getName().length()>1;
//                    }
//                }).forEach(author -> System.out.println(author));

        authors.stream()
                .filter(new Predicate<Author>() {
                    @Override
                    public boolean test(Author author) {
                        return author.getAge()>17;
                    }
                }.and(new Predicate<Author>() {
                    @Override
                    public boolean test(Author author) {
                        return author.getName().length()>1;
                    }
                })).forEach(author -> System.out.println(author));
    }

    private static void test26(){
        //	使用reduce求所有作者中年龄的最小值
        List<Author> authors = getAuthors();
        Optional<Integer> minoptional = authors.stream()
                .distinct()
                .map(author -> author.getAge())
                .reduce(new BinaryOperator<Integer>() {
                    @Override
                    public Integer apply(Integer result, Integer element) {
                        return result > element ? element : result;
                    }
                });
        minoptional.ifPresent(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) {
                System.out.println(integer);
            }
        });
    }

    private static void test25(){
        //	使用reduce求所有作者中年龄的最小直
        List<Author> authors = getAuthors();
        Integer min = authors.stream()
                .distinct()
                .map(author -> author.getAge())
                .reduce(Integer.MAX_VALUE, new BinaryOperator<Integer>() {
                    @Override
                    public Integer apply(Integer result, Integer element) {
                        return result > element ? element : result;
                    }
                });
        System.out.println(min);
    }
    private static void test24(){
        //	使用reduce求所有作者中年龄的最大值
        List<Author> authors = getAuthors();
        Integer max = authors.stream()
                .distinct()
                .map(author -> author.getAge())
                .reduce(Integer.MIN_VALUE, new BinaryOperator<Integer>() {
                    @Override
                    public Integer apply(Integer result, Integer element) {
                        return result < element ? element : result;
                    }
                });
        System.out.println(max);
    }
    private static void test23(){
        //对年龄求和
        List<Author> authors = getAuthors();
        Integer reduce = authors.stream()
                .distinct()
                .map(author -> author.getAge())
                .reduce(0, new BinaryOperator<Integer>() {
                    @Override
                    public Integer apply(Integer integer, Integer integer2) {
                        return integer + integer2;
                    }
                });
        System.out.println(reduce);

    }

    private static void test22(){
        //获取一个年龄最小的作家，并输出他的姓名。
        List<Author> authors = getAuthors();
        Optional<Author> firstAuthor = authors.stream()
                .distinct()
                .sorted(new Comparator<Author>() {
                    @Override
                    public int compare(Author o1, Author o2) {
                        return o1.getAge() - o2.getAge();
                    }
                })
                .findFirst();
        firstAuthor.ifPresent(new Consumer<Author>() {
            @Override
            public void accept(Author author) {
                System.out.println(author.getName());
            }
        });

    }


    private static void test21(){
        //	获取任意一个年龄大于18的作家，如果存在就输出他的名字
        List<Author> authors = getAuthors();
        Optional<Author> optionalAuthor = authors.stream()
                .filter(author -> author.getAge() > 18)
                .findAny();

        //这个也可以直接跟着后面点出来
        optionalAuthor.ifPresent(new Consumer<Author>() {
            @Override
            public void accept(Author author) {
                System.out.println(author.getName());
            }
        });
    }
    private static void test20(){
        //判断作家是否都没有超过100岁的。
        List<Author> authors = getAuthors();
        boolean flag = authors.stream()
                .noneMatch(new Predicate<Author>() {
                    @Override
                    public boolean test(Author author) {
                        return author.getAge() >= 100;
                    }
                });
        System.out.println(flag);

    }
    private static void test19(){
        //判断是否所有的作家都是成年人
        List<Author> authors = getAuthors();
        boolean flag = authors.stream()
                .allMatch(new Predicate<Author>() {
                    @Override
                    public boolean test(Author author) {
                        return author.getAge() >= 18;
                    }
                });
        System.out.println(flag);

    }

    private static void test18(){
        //	判断是否有年龄在29以上的作家
        List<Author> authors = getAuthors();
        boolean flag = authors.stream()
                .anyMatch(new Predicate<Author>() {
                    @Override
                    public boolean test(Author author) {
                        return author.getAge() > 29;
                    }
                });
        System.out.println(flag);


    }

    private static void test17(){
        //	获取一个Map集合，map的key为作者名，value为List<Book>。
        List<Author> authors = getAuthors();
        Map<String, List<Book>> map = authors.stream()
                .distinct()
                .collect(Collectors.toMap(new Function<Author, String>() {
                    @Override
                    public String apply(Author author) {
                        return author.getName();
                    }
                }, new Function<Author, List<Book>>() {
                    @Override
                    public List<Book> apply(Author author) {
                        return author.getBooks();
                    }
                }));
        System.out.println(map);
    }
    private static void test16(){
        // 获取一个所有书名的Set集合。
        List<Author> authors = getAuthors();
        Set<Book> books = authors.stream()
                .flatMap(author -> author.getBooks().stream())
                .collect(Collectors.toSet());
        System.out.println(books);
    }
    private static void test15(){
        //	获取一个存放所有作者名字的List集合。
        List<Author> authors = getAuthors();
        List<String> nameList = authors.stream()
                .map(author -> author.getName())
                .collect(Collectors.toList());
        System.out.println(nameList);
    }
    private static void test14(){
        //分别获取这些作家的所出书籍的最高分和最低分并打印。
        List<Author> authors =getAuthors();
        Optional<Integer> max = authors.stream()
                .flatMap(new Function<Author, Stream<Book>>() {
                    @Override
                    public Stream<Book> apply(Author author) {
                        return author.getBooks().stream();
                    }
                })
                .map(new Function<Book, Integer>() {
                    @Override
                    public Integer apply(Book book) {
                        return book.getScore();
                    }
                })
                .max(new Comparator<Integer>() {
                    @Override
                    public int compare(Integer o1, Integer o2) {
                        return o1 - o2;
                    }
                });



        Optional<Integer> min = authors.stream()
                .flatMap(new Function<Author, Stream<Book>>() {
                    @Override
                    public Stream<Book> apply(Author author) {
                        return author.getBooks().stream();
                    }
                })
                .map(new Function<Book, Integer>() {
                    @Override
                    public Integer apply(Book book) {
                        return book.getScore();
                    }
                })
                .min(new Comparator<Integer>() {
                    @Override
                    public int compare(Integer o1, Integer o2) {
                        return o1 - o2;
                    }
                });
        System.out.println(max.get());
        System.out.println(min.get());

    }
    private static void test13(){
        List<Author> authors =getAuthors();
        //打印这些作家的所出书籍的数目，注意删除重复元素。
        long count = authors.stream()
                .flatMap(author -> author.getBooks().stream())
                .distinct()
                .count();
        System.out.println(count);

    }
    private static void test12(){
        List<Author> authors =getAuthors();
        authors.stream()
                .map(new Function<Author, String>() {
                    @Override
                    public String apply(Author author) {
                        return author.getName();
                    }
                })
                .distinct()
                .forEach(new Consumer<String>() {
                    @Override
                    public void accept(String s) {
                        System.out.println(s);
                    }
                });

    }
    private static void test11(){
        //map只能把一个对象转换成另一个对象来作为流中的元素。而flatMap可以把一个对象转换成多个对象作为流中的元素。
        //打印现有数据的所有分类。要求对分类进行去重。不能出现这种格式：哲学,爱情
        List<Author> authors =getAuthors();
        authors.stream()
                .flatMap(new Function<Author, Stream<Book>>() {
                    @Override
                    public Stream<Book> apply(Author author) {
                        return author.getBooks().stream();
                    }
                })
                .distinct()
                .flatMap(new Function<Book, Stream<String>>() {
                    @Override
                    public Stream<String> apply(Book book) {
                        return Arrays.stream(book.getCategory().split(","));
                    }
                })
                .distinct()
                .forEach(new Consumer<String>() {
                    @Override
                    public void accept(String s) {
                        System.out.println(s);
                    }
                });
    }
    private static void test10(){
        //map只能把一个对象转换成另一个对象来作为流中的元素。而flatMap可以把一个对象转换成多个对象作为流中的元素。
        //打印所有书籍的名字。要求对重复的元素进行去重。
        List<Author> authors =getAuthors();
        authors.stream()
                .flatMap(new Function<Author, Stream<Book>>() {
                    @Override
                    public Stream<Book> apply(Author author) {
                        return author.getBooks().stream();
                    }
                })
                .distinct()
                .forEach(new Consumer<Book>() {
                    @Override
                    public void accept(Book book) {
                        System.out.println(book.getName());
                    }
                });

    }
    private static void test09(){
        //打印除了年龄最大的作家外的其他作家，要求不能有重复元素，并且按照年龄降序排序。
        List<Author> authors =getAuthors();
        authors.stream()
                .distinct()
                .sorted(new Comparator<Author>() {
                    @Override
                    public int compare(Author o1, Author o2) {
                        return o2.getAge()-o1.getAge();
                    }
                })
                .skip(1)
                .forEach(new Consumer<Author>() {
                    @Override
                    public void accept(Author author) {
                        System.out.println(author.getName());
                    }
                });
    }
    private static void test08(){
        //对流中的元素按照年龄进行降序排序，并且要求不能有重复的元素,然后打印其中年龄最大的两个作家的姓名。
        List<Author> authors =getAuthors();
        authors.stream()
                .distinct()
                .sorted(new Comparator<Author>() {
                    @Override
                    public int compare(Author o1, Author o2) {
                        return o2.getAge()-o1.getAge();
                    }
                })
                .limit(2)
                .forEach(new Consumer<Author>() {
                    @Override
                    public void accept(Author author) {
                        System.out.println(author.getName());
                    }
                });
    }
    private static void test07(){
        List<Author> authors =getAuthors();
        //对流中的元素按照年龄降序排序，并且要求不能有重复元素
//        authors.stream()
//                .distinct()
//                .sorted() //如果调用空参sort方法，则需要流中的元素实现了Comparable
//                .forEach(new Consumer<Author>() {
//                    @Override
//                    public void accept(Author author) {
//                        System.out.println(author.getAge());
//                    }
//                });
        authors.stream()
                .distinct()
                .sorted(new Comparator<Author>() {
                    @Override
                    public int compare(Author o1, Author o2) {
                        return o1.getAge()-o2.getAge();
                    }
                })
                .forEach(new Consumer<Author>() {
                    @Override
                    public void accept(Author author) {
                        System.out.println(author.getAge());
                    }
                });
    }
    private static void test06(){
        List<Author> authors =getAuthors();
        authors.stream()
                .distinct()
                .forEach(new Consumer<Author>() {
                    @Override
                    public void accept(Author author) {
                        System.out.println(author.getName());
                    }
                });
    }

    private static void test05(){
        List<Author> authors=getAuthors();
        //打印所有作家的名字
//        authors.stream()
//                .forEach(new Consumer<Author>() {
//                    @Override
//                    public void accept(Author author) {
//                        System.out.println(author.getName());
//                    }
//                });

//        authors.stream()
//                .map(author -> author.getName())
//                .forEach(s -> System.out.println(s));

        //map还可以做计算
        authors.stream()
                .map(author -> author.getAge())
                .map(age->age+10)
                .forEach(age-> System.out.println(age));
    }

    private static void test04(){
        List<Author> authors=getAuthors();
        //打印所有姓名长度大于1的作家的姓名
        authors.stream().distinct()
                .filter(new Predicate<Author>() {
                    @Override
                    public boolean test(Author author) {
                        return author.getName().length()>1;
                    }
                })
                .forEach(new Consumer<Author>() {
                    @Override
                    public void accept(Author author) {
                        System.out.println(author.getName());
                    }
                });
    }

    public static void test03() {

        Map<String,Integer> map = new HashMap<>();
        map.put("蜡笔小新",19);
        map.put("黑子",17);
        map.put("日向翔阳",16);

        //map作为双列集合：转换成单列集合后再创建
        Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
        Stream<Map.Entry<String, Integer>> stream = entrySet.stream();

        stream.filter(new Predicate<Map.Entry<String, Integer>>() {
            @Override
            public boolean test(Map.Entry<String, Integer> entry) {
                return entry.getValue()>16;
            }
        }).forEach(new Consumer<Map.Entry<String, Integer>>() {
            @Override
            public void accept(Map.Entry<String, Integer> entry) {
                System.out.println(entry.getKey()+"==="+entry.getValue());
            }
        });

    }


    private static void test02() {
        //数组变成stream流(2种方法)
        Integer [] arr = {1,2,3,4,5};
        Stream<Integer> stream_arr = Arrays.stream(arr);
        Stream<Integer> stream_arr2 = Stream.of(arr);
        stream_arr2.distinct()
                .filter(integer -> integer>2)
                .forEach(integer -> System.out.println(integer));
    }

    private static void test01() {
        List<Author> authors=getAuthors();
        //单列集合(list)变成stream流
        Stream<Author> stream = authors.stream();
        //打印所有年龄小于18的作家的名字，并且要注意去重
        authors.stream()
                .distinct()
                .filter(new Predicate<Author>() {
                    @Override
                    public boolean test(Author author) {
                        System.out.println("11");
                        return author.getAge() < 18;
                    }
                })
                .forEach(author -> System.out.println(author.getName()));
    }



    private static List<Author> getAuthors() {
        //数据初始化
        Author author = new Author(1L,"蒙多",33,"一个从菜刀中明悟哲理的祖安人",null);
        Author author2 = new Author(2L,"亚拉索",15,"狂风也追逐不上他的思考速度",null);
        Author author3 = new Author(3L,"易",14,"是这个世界在限制他的思维",null);
        Author author4 = new Author(3L,"易",14,"是这个世界在限制他的思维",null);

        //书籍列表
        List<Book> books1 = new ArrayList<>();
        List<Book> books2 = new ArrayList<>();
        List<Book> books3 = new ArrayList<>();

        books1.add(new Book(1L,"刀的两侧是光明与黑暗","哲学,爱情",88,"用一把刀划分了爱恨"));
        books1.add(new Book(2L,"一个人不能死在同一把刀下","个人成长,爱情",99,"讲述如何从失败中明悟真理"));

        books2.add(new Book(3L,"那风吹不到的地方","哲学",85,"带你用思维去领略世界的尽头"));
        books2.add(new Book(3L,"那风吹不到的地方","哲学",85,"带你用思维去领略世界的尽头"));
        books2.add(new Book(4L,"吹或不吹","爱情,个人传记",56,"一个哲学家的恋爱观注定很难把他所在的时代理解"));

        books3.add(new Book(5L,"你的剑就是我的剑","爱情",56,"无法想象一个武者能对他的伴侣这么的宽容"));
        books3.add(new Book(6L,"风与剑","个人传记",100,"两个哲学家灵魂和肉体的碰撞会激起怎么样的火花呢？"));
        books3.add(new Book(6L,"风与剑","个人传记",100,"两个哲学家灵魂和肉体的碰撞会激起怎么样的火花呢？"));

        author.setBooks(books1);
        author2.setBooks(books2);
        author3.setBooks(books3);
        author4.setBooks(books3);

        List<Author> authorList = new ArrayList<>(Arrays.asList(author,author2,author3,author4));
        return authorList;
    }
}
